package com.wanmait.zaisousuo.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.zaisousuo.pojo.*;
import com.wanmait.zaisousuo.service.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/backstage/garbage")
public class garbageController {

    @Resource
    private GarbageService garbageService;
    @Resource
    private UserinfoService userinfoService;
    @Resource
    private SolutionService solutionService;
    @Resource
    private GarbageTypeService garbageTypeService;
    @Resource
    private TextureService textureService;
    @Resource
    private CompanyDataService companyDataService;

    //垃圾列表页面
    @RequestMapping("garbageList")
    public void showGarbage(){
    }

    //垃圾数据页面
    @RequestMapping("/garbageData")
    public ModelAndView toGarbageData(Integer state){
        ModelAndView mv=new ModelAndView();
        mv.addObject("allGarbage",garbageService.findAllGarbageByState(state));
        return mv;
    }

    //材质数据页面
    @RequestMapping("/textureData")
    public void toTextureData(Integer state,Model model){
        model.addAttribute("allTexture",textureService.listAllTexture(state));
    }

    //垃圾详情页面
    @RequestMapping("/information")
    public void information(Model model,Integer id){
        Garbage garbage=(Garbage)garbageService.findById(id);
        model.addAttribute("garbage",garbage);
    }

    //去往发布者用户信息页面
    @RequestMapping("/userinfo")
    public void toUserinfor(Model model,Integer id){
        model.addAttribute("userinfo",userinfoService.findById(id));
    }

    //去往垃圾处理方式页面
    @RequestMapping("/solution")
    public void toSolution(Model model,Integer id){
        model.addAttribute("allSolution",solutionService.findAllSolutionById(id));
        model.addAttribute("garbage",garbageService.findById(id));
    }

    //删除处理方式
    @RequestMapping("/deleteSolution")
    @ResponseBody
    public void deleteSolution(Integer id,Integer garbageId){
        garbageService.clearCache();
        solutionService.delete(id);
        garbageService.updateReduceCountById(garbageId);
    }

    //删除垃圾
    @RequestMapping("/deleteGarbage")
    @ResponseBody
    public  void deleteGarbage(Integer id){
        garbageService.clearCache();
        garbageService.delete(id);
    }

    //更改审核状态
    @RequestMapping("/changeState")
    @ResponseBody
    public void changeState(Boolean flag,Integer id){
        garbageService.clearCache();
        garbageService.changeState(flag,id);
    }

    //去往编辑垃圾信息页面
    @RequestMapping("/edit")
    public void edit(Model model,Integer id){
        model.addAttribute("garbage",garbageService.findById(id));
        model.addAttribute("allGarbageType",garbageTypeService.findAll());
        model.addAttribute("garbageParentTexture",textureService.findAll());
    }

    //存放根据一级材质找到的二级材质
    @RequestMapping("/nextTextureData")
    @ResponseBody
    public ModelAndView nextTextureData(Integer textureId) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("textures", textureService.findChildrenTexture(textureId));
        return mv;
    }

    //更新垃圾信息，主要是图片，简介，分类，名字
    @RequestMapping("/updateGarbage")
    public String updateGarbage(MultipartFile file, Garbage garbage, HttpServletRequest request){
        garbageService.clearCache();
        //如果没有更换图片，还用以前的图片
        if(file.getOriginalFilename().equals("")){
            garbage.setPicture(garbageService.findById(garbage.getId()).getPicture());
            garbageService.update(garbage);
            return "redirect:/backstage/garbage/garbageList";
        }
        //添加了新的图片，重新装图片
        else{
            String ext = FilenameUtils.getExtension(file.getOriginalFilename());
            String fileName = UUID.randomUUID() + "." + ext;
            String realPath = request.getServletContext().getRealPath("/static/images") + "/" + fileName;
            try {
                file.transferTo(new File(realPath));
                garbage.setPicture(fileName);
                garbageService.update(garbage);
                return "redirect:/backstage/garbage/garbageList";
            } catch (IOException e) {
                return "redirect:/backstage/garbage/garbageList";
            }
        }
    }

    //更新材质信息，主要是图片，分类，名字
    @RequestMapping("/updateTexture")
    public String updateTexture(MultipartFile file, Texture texture, HttpServletRequest request){
        textureService.clearCache();
        //如果没有更换图片，还用以前的图片
        if(file.getOriginalFilename().equals("")){
            texture.setInco(textureService.findById(texture.getId()).getInco());
            textureService.update(texture);
            return "redirect:/backstage/garbage/textureList";
        }
        //添加了新的图片，重新装图片
        else{
            String ext = FilenameUtils.getExtension(file.getOriginalFilename());
            String fileName = UUID.randomUUID() + "." + ext;
            String realPath = request.getServletContext().getRealPath("/static/images") + "/" + fileName;
            try {
                file.transferTo(new File(realPath));
                texture.setInco(fileName);
                textureService.update(texture);
                return "redirect:/backstage/garbage/textureList";
            } catch (IOException e) {
                return "redirect:/backstage/garbage/textureList";
            }
        }
    }

    //去往垃圾材质管理页面
    @RequestMapping("/textureList")
    public void toTextureList(Model model){
    }

    //更改材质的状态
    @RequestMapping("/changeTextureState")
    public void changeTextureState(Boolean flag,Integer id){
        textureService.clearCache();
        textureService.changeTextureState(flag,id);
    }

    //更改分类的状态
    @RequestMapping("/changeTypeState")
    public void changeTypeState(Boolean flag,Integer id){
        garbageTypeService.clearCache();
        garbageTypeService.changeTypeState(flag,id);
    }

    //去往添加材质的页面,一级材质传过来true,页面不用选择一级，二级传过来false，页面选择一级
    @RequestMapping("/addTexture")
    public void addTexture(Boolean flag,Model model){
        if(flag==true){}
        if(flag==false){
            model.addAttribute("garbageParentTexture",textureService.findAll());
        }
        model.addAttribute("flag",flag);
}

   //往数据库中添加材质
    @RequestMapping("/insertTexture")
    public String insertTexture(Texture texture,MultipartFile file,HttpServletRequest request,Boolean flag){
        textureService.clearCache();
        String ext = FilenameUtils.getExtension(file.getOriginalFilename());
        String fileName = UUID.randomUUID() + "." + ext;
        String realPath = request.getServletContext().getRealPath("/static/images") + "/" + fileName;
        try {
            file.transferTo(new File(realPath));
            texture.setInco(fileName);
            texture.setDisabled(false);
            textureService.insert(texture);
            return "redirect:/backstage/garbage/addTexture?flag="+flag+"";
        } catch (IOException e) {
            return "redirect:/backstage/garbage/addTexture?flag="+flag+"";
        }
    }

    //去往垃圾材质编辑页面
    @RequestMapping("/textureEdit")
    public void toTextureEdit(Model model,Integer id){
        model.addAttribute("allTexture",textureService.findAll());
        model.addAttribute("texture",textureService.findById(id));
    }

    //去往分类页面
    @RequestMapping("/typeList")
    public void toTypeList(Model model){
        model.addAttribute("allType",garbageTypeService.findAll2());
    }

    //去往添加分类的页面
    @RequestMapping("/addType")
    public void toAddType(){

    }

    //往数据库中加入分类
    @RequestMapping("insertType")
    public String insertType(GarbageType garbageType,HttpServletRequest request,MultipartFile file){
        String ext = FilenameUtils.getExtension(file.getOriginalFilename());
        String fileName = UUID.randomUUID() + "." + ext;
        String realPath = request.getServletContext().getRealPath("/static/images") + "/" + fileName;
        try {
            file.transferTo(new File(realPath));
            garbageType.setInco(fileName);
            garbageTypeService.insert(garbageType);
            return "redirect:/backstage/garbage/addType";
        } catch (IOException e) {
            return "redirect:/backstage/garbage/addType";
        }

    }

    //去往垃圾数据管理页面
    @RequestMapping("/dataList")
    public void toDataList(Model model){
        List<CompanyData> datas=companyDataService.findAll();
//        List<String> strs=null;
//        for(int i=0;i<datas.size();i++){
//            Date date=datas.get(i).getEventTime();
//            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//            String rightDate=sdf.format(date);
//        }
      model.addAttribute("datas",companyDataService.findAll());
    }

    //更改垃圾数据的发布状态，flag是false改为1
    @RequestMapping("/changeDataState")
    @ResponseBody
    public void changeDataState(Boolean flag,Integer id){
        companyDataService.clearFindAllCompanyDataCache();//清缓存
        companyDataService.changeStateById(flag,id);
    }

    //删除一条数据
    @RequestMapping("/deleteData")
    @ResponseBody
    public void deleteData(Integer id){
        companyDataService.clearFindAllCompanyDataCache();//清缓存
        companyDataService.delete(id);
    }

    //删除一条材质
    @RequestMapping("/deleteTexture")
    @ResponseBody
    public void deleteTexture(Integer id){
        textureService.delete(id);
    }


}
